<%@ page import="org.springframework.web.context.request.SessionScope" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%request.setAttribute("ctx", request.getContextPath());%>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="/layui-v2.9.14/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/jquery-2.1.0.min.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/layui.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/xm-select.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/crypto-js.js"></script>
    <title></title>
</head>
<body>
<form class="layui-form" action="">
    <div class="layui-col-md4">
        <label class="layui-form-label">车牌号码</label>
        <div class="layui-input-block">
            <input type="text" name="plateNumber" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-col-md4">
        <label class="layui-form-label">月卡卡号</label>
        <div class="layui-input-block">
            <input type="text" name="cardNumber" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-col-md4">
        <label class="layui-form-label">车位号</label>
        <div class="layui-input-block">
            <input type="text" name="parkNum" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-col-md4">
        <label class="layui-form-label">月卡类型</label>
        <div class="layui-input-block">
            <select name="cardType">
                <option value="">请选择</option>
                <option value="包月">包月</option>
                <option value="包季">包季</option>
                <option value="包年">包年</option>
            </select>
        </div>
    </div>
    <div class="layui-col-md4">
        <label class="layui-form-label">月卡时长</label>
        <div class="layui-input-block">
            <select name="duration">
                <option value="">请选择</option>
                <option value="1个月">1个月</option>
                <option value="2个月">2个月</option>
                <option value="3个月">3个月</option>
                <option value="半年">半年</option>
                <option value="1年">1年</option>
                <option value="长期">长期</option>
            </select>
        </div>
    </div>
    <div class="layui-col-md4">
        <label class="layui-form-label">自定义时长</label>
        <div class="layui-input-block">
            <input type="text" name="customDuration" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-col-md4">
        <label class="layui-form-label">车主姓名</label>
        <div class="layui-input-block">
            <input type="text" name="ownerName" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-col-md4">
        <label class="layui-form-label">车主电话</label>
        <div class="layui-input-block">
            <input type="text" name="ownerPhone" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-col-md4">
        <label class="layui-form-label">开始时间</label>
        <div class="layui-input-block">
            <input type="text" name="startDate" class="layui-input mydate"  placeholder="yyyy-MM-dd">
        </div>
    </div>
    <div class="layui-col-md4">
        <label class="layui-form-label">结束时间</label>
        <div class="layui-input-block">
            <input type="text" name="endDate" class="layui-input mydate"  placeholder="yyyy-MM-dd">
        </div>
    </div>
    <div class="layui-col-md4">
        <label class="layui-form-label">车牌类型</label>
        <div class="layui-input-block">
            <select name="plateTypeId">
                <option value="">请选择</option>

            </select>
        </div>
    </div>
    <div class="layui-col-md4">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <input type="text" name="bz" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-col-md4">
    <div class="layui-input-block">
        <input type="radio" name="isActive" value="正常" title="正常" checked>
        <input type="radio" name="isActive" value="过期" title="过期">
        <input type="radio" name="isActive" value="暂停" title="暂停" >
    </div>

    </div>


    <div class="layui-form-item">

        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>


<script>
    layui.use(['form', 'laydate', 'util'], function(){
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var util = layui.util;


        //获取所有车牌分类
        $.get("/car/listPlateType.do", function (res) {
            let data = res.data;
            for (let i = 0; i < data.length; i++) {
                $("[name='plateTypeId']").append("<option value='" + data[i].id + "'>" + data[i].color  + data[i].textColor+ "</option>");
            }
            form.render('select');//渲染下拉框
        })

        // 提交事件
        form.on('submit(demo1)', function(data){
            var field = data.field; // 获取表单字段值

            //计算开始和结束日期---这里只是给学生演示rq的处理比较
          let date1=  new Date(field.startDate).setHours(0,0,0,0);
          let date2=  new Date(field.endDate).setHours(0,0,0,0);
          //获取两个日期之间的天数
           let date =(date2-date1)/1000/60/60/24;


            //校验车牌号是否存在,提示
            $.get("/car/checkPlateNumber.do",field,function (res){
                if(res.code!=0){//存在则提示
                    layer.msg(res.msg,{time:2000})
                }else{
                    //校验月卡已经被绑定了,提示
                    $.get("/car/checkMontylyCard.do",field,function (res){
                        //卡号已经存在
                        let card=res.data
                        if(card!=null && card.carId!=null){//存在则提示
                            layer.msg("该月卡被使用",{time:2000})
                        }else{
                            if(card!=null){
                                layer.msg("该月卡已经存在,但未绑定车辆,可以使用",{time:2000});
                                //数据回显
                            //如果月卡存在, 则赋值数据,目的是表示修改月卡
                                field.id=card.id;
                            }
                            //提交表单
                            $.post("/car/add.do",field,function (res){

                                if(res.code==0){
                                    layer.msg(res.msg,{time:2000},function (){
                                        location.href="/car/tolist.do";
                                    })
                                }else{
                                    layer.msg(res.msg,{time:2000});
                                }
                            })
                        }
                    })

                }
            })

            return false; // 阻止默认 form 跳转
        });

        // 日期
        laydate.render({
            elem: '.mydate'
        });


    });
</script>

</body>
</html>